/**
 * OWASP GoatDroid Project
 * 
 * This file is part of the Open Web Application Security Project (OWASP)
 * GoatDroid project. For details, please see
 * https://www.owasp.org/index.php/Projects/OWASP_GoatDroid_Project
 *
 * Copyright (c) 2011 - The OWASP Foundation
 * 
 * GoatDroid is published by OWASP under the GPLv3 license. You should read and accept the
 * LICENSE before you use, modify, and/or redistribute this software.
 * 
 * @author Jack Mannino, nVisium Security (https://www.nvisiumsecurity.com)
 * @created 2011
 */
package org.owasp.goatdroid.gui.emulator;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.owasp.goatdroid.gui.config.Config;
import org.owasp.goatdroid.gui.exception.CorruptConfigException;

public class EmulatorRunnable implements Runnable {

	String deviceName;
	static String slash;

	public EmulatorRunnable(String deviceName) {

		/*Windows = \
		 * OSX/Linux = /
		 */
		if (System.getProperty("os.name").toLowerCase().startsWith("windows"))
			slash = File.separator + File.separator;
		else
			slash = File.separator;

		String[] splitDeviceName = deviceName.split(slash);
		this.deviceName = splitDeviceName[splitDeviceName.length - 1];
	}

	@Override
	public void run() {
		try {
			startEmulator(deviceName);
		} catch (FileNotFoundException e) {
			e.getMessage();
		} catch (IOException e) {
			e.getMessage();
		} catch (CorruptConfigException e) {
			e.getMessage();
		}
		Thread.yield();
	}

	static public void startEmulator(String deviceName)
			throws FileNotFoundException, IOException, CorruptConfigException {
		Config config = new Config();
		HashMap<String, String> settings = config.readConfig();
		String emulator = "";
		if (System.getProperty("os.name").toLowerCase().startsWith("windows"))
			emulator = "\"" + settings.get("sdk_path") + "\\" + "tools" + "\\"
					+ "emulator-arm.exe\"";
		else
			emulator = settings.get("sdk_path") + slash + "tools" + slash
					+ "emulator";
		CommandLine cmdLine = new CommandLine(emulator);
		// If the Windows path has spaces, we don't even want to deal with it
		// will ignore parameters, known issue
		if (System.getProperty("os.name").toLowerCase().startsWith("windows"))
			cmdLine.addArgument("@"
					+ deviceName.toLowerCase().replace(".avd", ""));
		else {
			cmdLine.addArgument("-avd", false);
			cmdLine.addArgument(deviceName.toLowerCase().replace(".avd", ""),
					false);
		}
		if (!(settings.get("proxy_host").equals("") || settings.get(
				"proxy_port").equals(""))) {
			cmdLine.addArgument("-http-proxy");
			cmdLine.addArgument(settings.get("proxy_host") + ":"
					+ settings.get("proxy_port"));
		}
		DefaultExecutor executor = new DefaultExecutor();
		try {
			int exitValue = executor.execute(cmdLine);
			exitValue += 0;
		} catch (ExecuteException e) {
			e.getMessage();
		} catch (IOException e) {
			e.getMessage();
		}
	}
}
